﻿<Menu Class="aside-container menu-site" Theme="MenuTheme.Light" Mode="MenuMode.Inline">
    @menuItemTemplate((MenuItems, LanguageService, 1))
</Menu>


@inject ILanguageService LanguageService
@inject NavigationManager NavigationManager
@inject HttpClient HttpClient

@code {

    [Parameter]
    public bool IsCollapsed { get; set; }

    [Parameter]
    public DemoMenuItem[] MenuItems { get; set; } = {};

    static readonly RenderFragment<(DemoMenuItem[] items, ILanguageService languageService, int level)> menuItemTemplate = context =>
        @<Template>
            @foreach (var menu in context.items)
            {
                if (menu.Type == "menuItem")
                {
                        <MenuItem RouterLink="@($"{context.languageService.CurrentCulture.Name}/{menu.Url}")" RouterMatch="@NavLinkMatch.All">
                            <span>@menu.Title</span>
                            @if (menu.SubTitle != null)
                            {
                                <span class="chinese">@menu.SubTitle</span>
                            }
                            else if (context.level > 1 && context.languageService.CurrentCulture.Name == "zh-CN")
                            {
                                <span class="chinese">@(context.languageService.Resources[menu.Title])</span>
                            }
                        </MenuItem>
                }
                else if (menu.Type == "subMenu")
                {
                    <SubMenu Title="@(context.languageService.Resources[menu.Title])" IsOpen="true">
                        @if (menu.Children.Any())
                        {
                            @menuItemTemplate((menu.Children, context.languageService, context.level + 1))
                        }
                    </SubMenu>
                }
                else if (menu.Type == "itemGroup")
                {
                    <MenuItemGroup Title="@context.languageService.Resources[menu.Title]">
                        @if (menu.Children.Any())
                        {
                            @menuItemTemplate((menu.Children, context.languageService, context.level + 1))
                        }
                    </MenuItemGroup>
                }
            }
        </Template>;

    protected override async Task OnInitializedAsync()
    {
        var baseUrl = NavigationManager.ToAbsoluteUri(NavigationManager.BaseUri);
        MenuItems = await HttpClient.GetFromJsonAsync<DemoMenuItem[]>(new Uri(baseUrl, "_content/AntDesign.Docs/menu.json").ToString());

        LanguageService.LanguageChanged += (_, args) =>
        {
            InvokeAsync(StateHasChanged);
        };

        NavigationManager.LocationChanged += (_, args) =>
        {
            StateHasChanged();
        };
        StateHasChanged();
        await base.OnInitializedAsync();
    }
}
